package com.victor.utils.jiguang.jmessage;

import java.util.ArrayList;
import java.util.List;

import cn.jiguang.common.resp.ResponseWrapper;
import cn.jmessage.api.JMessageClient;
import cn.jmessage.api.common.model.RegisterInfo;
import cn.jmessage.api.common.model.RegisterPayload;
import cn.jmessage.api.common.model.UserPayload;
import cn.jmessage.api.resource.ResourceClient;
import cn.jmessage.api.resource.UploadResult;
import cn.jmessage.api.user.UserClient;
import cn.jmessage.api.user.UserInfoResult;

public class JiguangUtil {
	// 极光IM
	private static final String appKey = "adf72cfa74361ab1278b91ff";
	private static final String masterSecret = "aba6e6673077c2756d984c86";

	public static void main(String[] args) {
		String result_jgregister = JiguangUtil.regJiGuangUser("Asd18704072222", "Asd12345678", "社会你小彬哥1",
				"E:\\afeier.jpg");
		System.out.println(result_jgregister);
	}

	/**
	 * 注册极光im用户
	 */
	// String result_jgregister = JiguangUtil.regJiGuangUser(hxid, hxpassword,
	// "nickname", "C:\\apache-tomcat-7.0.81\\webapps\\image\\headphoto.png");
	// if (result_jgregister.equals("fail")) {
	// return Msg.fail().add("data", "极光账号注册失败");
	// }

	// 注册用户
	@SuppressWarnings("unused")
	public static String regJiGuangUser(String username, String password, String nickname, String headphoto) {
		UserClient client = new UserClient(appKey, masterSecret);
		try {
			List<RegisterInfo> users = new ArrayList<RegisterInfo>();
			RegisterInfo user = RegisterInfo.newBuilder().setUsername(username).setPassword(password)
					.setNickname(nickname).build();
			users.add(user);
			RegisterInfo[] regUsers = new RegisterInfo[users.size()];

			RegisterPayload payload = RegisterPayload.newBuilder().addUsers(users.toArray(regUsers)).build();
			ResponseWrapper registerUsers = client.registerUsers(payload);

			String photourl = uploadJiGuangFile(headphoto);
			if (photourl.equals("fail")) {
				return "fail";
			}
			UserPayload payload2 = UserPayload.newBuilder().setAvatar(photourl).build();
			ResponseWrapper registerUsers2 = client.updateUserInfo(username, payload2);
//			System.out.println(registerUsers2);// {"responseCode":204,"responseContent":"","rateLimitQuota":600,"rateLimitRemaining":598,"rateLimitReset":53}
//			System.out.println(registerUsers);
		} catch (Exception e) {
			e.printStackTrace();
			return "fail";
		}
		return "success";
	}

	// 上传文件到极光服务器
	public static String uploadJiGuangFile(String photourl) {
		ResourceClient client = new ResourceClient(appKey, masterSecret);
		try {
			UploadResult result = client.uploadFile(photourl);
			String mediaId = result.getMediaId();
			return mediaId;
		} catch (Exception e) {
			return "fail";
		}
	}

	// 获取极光用户信息
	public static UserInfoResult getUserInfo(String jg_username) {
		JMessageClient client = new JMessageClient(appKey, masterSecret);
		try {
			UserInfoResult res = client.getUserInfo(jg_username);
			return res;
		} catch (Exception e) {
			return null;
		}
	}

	// 修改极光用户昵称
	public static String updateJiGuangNickname(String jg_username, String jg_nickname) {
		JMessageClient client = new JMessageClient(appKey, masterSecret);

		UserInfoResult jg_user = getUserInfo(jg_username);
		if (jg_user == null) {
			return "fail";
		}
		try {
			client.updateUserInfo(jg_username, jg_nickname, "2000-01-01", "", 1, "", "", jg_user.getAvatar());
		} catch (Exception e) {
			return "fail";
		}
		return "success";
	}

	// 修改极光用户头像
	public static String updateJiGuangHeadphoto(String jg_username, String photourl) {
		JMessageClient client = new JMessageClient(appKey, masterSecret);
		if (photourl.equals("fail")) {
			return "fail";
		}
		UserInfoResult jg_user = getUserInfo(jg_username);
		if (jg_user == null) {
			return "fail";
		}
		try {
			client.updateUserInfo(jg_username, jg_user.getNickname(), "2000-01-01", "", 1, "", "", photourl);
		} catch (Exception e) {
			return "fail";
		}
		return "success";
	}

}